
package com.bening.smsapp.parser.thread;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PoolTester {

   private static final Log log = LogFactory.getLog(PoolTester.class);

   private ThreadPool pool = null;

   /**
    * @return Returns the pool.
    */
   public ThreadPool getPool() {
      return pool;
   }

   /**
    * @param pool - The pool to set.
    */
   public void setPool(ThreadPool tpool) {
      this.pool = tpool;
   }

   public PoolTester() {
      if (pool == null) {
         pool = new ThreadPool(new ThreadObjectFactory());
         
      }
   }

   /**
    * shutdown 
    */
   public void shutdown() {
      try {
         pool.close();
      } catch (Exception e) {
         log.error("", e);
      }
   }

   public void runAsyncTask() {
      try {
         log.debug("**** Running runAsyncTask().. ");
         WorkerThread rt1 = (WorkerThread) pool.borrowObject();
         rt1.setPool(pool); 
     //    rt1.execute("com.findonnet.services.pooling.test.SampleWork",
     //          "executeTask", null, null, null);
      } catch (Exception e) {
         log.error("", e);
      }
   }


   public static void main(String[] args) {
      
      PoolTester pt = new PoolTester();
      pt.runAsyncTask(); // run asynchronously without waiting for results.

      try {
         Thread.sleep(8000); // sleep for 8 secs
      } catch (InterruptedException e) {
         log.error("", e);
      }

      System.out.println(" \n    <<<< Shutting down pool >>> ");
      pt.shutdown();
      System.out.println("\n     <<<< Finished Shutting down pool >>> ");
      System.out.println("\n\n********* Finished Test *************************");
   }

}